package com.love.marriage.regionInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.regionInfo.entity.RegionInfo;
import com.love.marriage.regionInfo.mapper.RegionInfoMapper;
import com.love.marriage.regionInfo.service.RegionInfoService;
import com.love.marriage.regionInfo.vo.RegionDetailInfoVo;
import com.love.marriage.regionInfo.vo.RegionInfoVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  省市区信息表服务实现类
 * author:       alex
 * createDate:   2024-10-13 21:41:13
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class RegionInfoServiceImp extends ServiceImpl<RegionInfoMapper, RegionInfo> implements RegionInfoService {

    private final RegionInfoMapper regionInfoMapper;

    @Override
    public Page<RegionInfoVo> getPage(Long pageNum, Long pageSize, RegionInfoVo regionInfoVo) {
        Page<RegionInfoVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return regionInfoMapper.getPage(page, regionInfoVo);
    }

    @Override
    public List<RegionInfoVo> getList(RegionInfoVo regionInfoVo) {
        return regionInfoMapper.getList(regionInfoVo);
    }

    @Override
    public RegionInfoVo queryRegionInfo(Long id) {
        return regionInfoMapper.queryRegionInfo(id);
    }

    @Override
    public RegionInfoVo addRegionInfo(RegionInfoVo regionInfoVo) {
        RegionInfo regionInfo = new RegionInfo();
        BeanUtils.copyProperties(regionInfoVo, regionInfo);
        regionInfoMapper.insert(regionInfo);
        RegionInfoVo res = new RegionInfoVo();
        BeanUtils.copyProperties(regionInfo, res);
        return res;
    }

    @Override
    public RegionInfoVo updateRegionInfo(RegionInfoVo regionInfoVo) {
        RegionInfo regionInfo = new RegionInfo();
        BeanUtils.copyProperties(regionInfoVo, regionInfo);
        regionInfoMapper.updateById(regionInfo);
        return regionInfoVo;
    }

    @Override
    public Boolean deleteRegionInfo(String ids) {
        if(StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        regionInfoMapper.deleteBatchIds(idArr);
        return true;
    }

    @Override
    public RegionDetailInfoVo reginDetailInfo(String provinceName, String cityName, String districtName) {
        return regionInfoMapper.reginDetailInfo(provinceName, cityName, districtName);
    }
}
